﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace TuLing.Blazor.Migrations
{
    public partial class removeFields223 : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_BuyOrderReturns_Customers_CustomerId",
                table: "BuyOrderReturns");

            migrationBuilder.DropForeignKey(
                name: "FK_BuyOrders_Customers_CustomerId",
                table: "BuyOrders");

            migrationBuilder.DropForeignKey(
                name: "FK_SaleOrderDetails_Customers_CustomerId",
                table: "SaleOrderDetails");

            migrationBuilder.DropForeignKey(
                name: "FK_SaleOrderDetails_SaleOrders_SaleOrderId",
                table: "SaleOrderDetails");

            migrationBuilder.DropIndex(
                name: "IX_SaleOrderDetails_CustomerId",
                table: "SaleOrderDetails");

            migrationBuilder.DropColumn(
                name: "Amount",
                table: "SaleOrders");

            migrationBuilder.DropColumn(
                name: "Quantity",
                table: "SaleOrders");

            migrationBuilder.DropColumn(
                name: "Code",
                table: "SaleOrderDetails");

            migrationBuilder.DropColumn(
                name: "Name",
                table: "SaleOrderDetails");

            migrationBuilder.RenameColumn(
                name: "CustomerId",
                table: "SaleOrderDetails",
                newName: "Quantity");

            migrationBuilder.RenameColumn(
                name: "Amount",
                table: "SaleOrderDetails",
                newName: "UnitPrice");

            migrationBuilder.RenameColumn(
                name: "CustomerId",
                table: "BuyOrders",
                newName: "SupplierId");

            migrationBuilder.RenameIndex(
                name: "IX_BuyOrders_CustomerId",
                table: "BuyOrders",
                newName: "IX_BuyOrders_SupplierId");

            migrationBuilder.RenameColumn(
                name: "CustomerId",
                table: "BuyOrderReturns",
                newName: "SupplierId");

            migrationBuilder.RenameIndex(
                name: "IX_BuyOrderReturns_CustomerId",
                table: "BuyOrderReturns",
                newName: "IX_BuyOrderReturns_SupplierId");

            migrationBuilder.AlterColumn<long>(
                name: "SaleOrderId",
                table: "SaleOrderDetails",
                type: "INTEGER",
                nullable: true,
                oldClrType: typeof(long),
                oldType: "INTEGER");

            migrationBuilder.AddColumn<long>(
                name: "BuyOrderId",
                table: "SaleOrderDetails",
                type: "INTEGER",
                nullable: false,
                defaultValue: 0L);

            migrationBuilder.AddColumn<long>(
                name: "ProductId",
                table: "SaleOrderDetails",
                type: "INTEGER",
                nullable: false,
                defaultValue: 0L);

            migrationBuilder.AddColumn<long>(
                name: "SaleOrderReturnId",
                table: "BuyOrderReturnDetails",
                type: "INTEGER",
                nullable: true);

            migrationBuilder.CreateTable(
                name: "SaleOrderReturns",
                columns: table => new
                {
                    Id = table.Column<long>(type: "INTEGER", nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    Code = table.Column<string>(type: "TEXT", nullable: true),
                    Name = table.Column<string>(type: "TEXT", nullable: true),
                    Remark = table.Column<string>(type: "TEXT", nullable: true),
                    CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
                    CustomerId = table.Column<long>(type: "INTEGER", nullable: false),
                    BuyOrderId = table.Column<long>(type: "INTEGER", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_SaleOrderReturns", x => x.Id);
                    table.ForeignKey(
                        name: "FK_SaleOrderReturns_Customers_CustomerId",
                        column: x => x.CustomerId,
                        principalTable: "Customers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "SaleOrderReturnDetails",
                columns: table => new
                {
                    Id = table.Column<long>(type: "INTEGER", nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    Quantity = table.Column<int>(type: "INTEGER", nullable: false),
                    UnitPrice = table.Column<decimal>(type: "TEXT", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
                    Remark = table.Column<string>(type: "TEXT", nullable: true),
                    SaleOrderReturnId = table.Column<long>(type: "INTEGER", nullable: false),
                    ProductId = table.Column<long>(type: "INTEGER", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_SaleOrderReturnDetails", x => x.Id);
                    table.ForeignKey(
                        name: "FK_SaleOrderReturnDetails_Products_ProductId",
                        column: x => x.ProductId,
                        principalTable: "Products",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_SaleOrderReturnDetails_SaleOrderReturns_SaleOrderReturnId",
                        column: x => x.SaleOrderReturnId,
                        principalTable: "SaleOrderReturns",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_SaleOrderDetails_BuyOrderId",
                table: "SaleOrderDetails",
                column: "BuyOrderId");

            migrationBuilder.CreateIndex(
                name: "IX_SaleOrderDetails_ProductId",
                table: "SaleOrderDetails",
                column: "ProductId");

            migrationBuilder.CreateIndex(
                name: "IX_BuyOrderReturnDetails_SaleOrderReturnId",
                table: "BuyOrderReturnDetails",
                column: "SaleOrderReturnId");

            migrationBuilder.CreateIndex(
                name: "IX_SaleOrderReturnDetails_ProductId",
                table: "SaleOrderReturnDetails",
                column: "ProductId");

            migrationBuilder.CreateIndex(
                name: "IX_SaleOrderReturnDetails_SaleOrderReturnId",
                table: "SaleOrderReturnDetails",
                column: "SaleOrderReturnId");

            migrationBuilder.CreateIndex(
                name: "IX_SaleOrderReturns_CustomerId",
                table: "SaleOrderReturns",
                column: "CustomerId");

            migrationBuilder.AddForeignKey(
                name: "FK_BuyOrderReturnDetails_SaleOrderReturns_SaleOrderReturnId",
                table: "BuyOrderReturnDetails",
                column: "SaleOrderReturnId",
                principalTable: "SaleOrderReturns",
                principalColumn: "Id");

            migrationBuilder.AddForeignKey(
                name: "FK_BuyOrderReturns_Suppliers_SupplierId",
                table: "BuyOrderReturns",
                column: "SupplierId",
                principalTable: "Suppliers",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_BuyOrders_Suppliers_SupplierId",
                table: "BuyOrders",
                column: "SupplierId",
                principalTable: "Suppliers",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_SaleOrderDetails_BuyOrders_BuyOrderId",
                table: "SaleOrderDetails",
                column: "BuyOrderId",
                principalTable: "BuyOrders",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_SaleOrderDetails_Products_ProductId",
                table: "SaleOrderDetails",
                column: "ProductId",
                principalTable: "Products",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_SaleOrderDetails_SaleOrders_SaleOrderId",
                table: "SaleOrderDetails",
                column: "SaleOrderId",
                principalTable: "SaleOrders",
                principalColumn: "Id");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_BuyOrderReturnDetails_SaleOrderReturns_SaleOrderReturnId",
                table: "BuyOrderReturnDetails");

            migrationBuilder.DropForeignKey(
                name: "FK_BuyOrderReturns_Suppliers_SupplierId",
                table: "BuyOrderReturns");

            migrationBuilder.DropForeignKey(
                name: "FK_BuyOrders_Suppliers_SupplierId",
                table: "BuyOrders");

            migrationBuilder.DropForeignKey(
                name: "FK_SaleOrderDetails_BuyOrders_BuyOrderId",
                table: "SaleOrderDetails");

            migrationBuilder.DropForeignKey(
                name: "FK_SaleOrderDetails_Products_ProductId",
                table: "SaleOrderDetails");

            migrationBuilder.DropForeignKey(
                name: "FK_SaleOrderDetails_SaleOrders_SaleOrderId",
                table: "SaleOrderDetails");

            migrationBuilder.DropTable(
                name: "SaleOrderReturnDetails");

            migrationBuilder.DropTable(
                name: "SaleOrderReturns");

            migrationBuilder.DropIndex(
                name: "IX_SaleOrderDetails_BuyOrderId",
                table: "SaleOrderDetails");

            migrationBuilder.DropIndex(
                name: "IX_SaleOrderDetails_ProductId",
                table: "SaleOrderDetails");

            migrationBuilder.DropIndex(
                name: "IX_BuyOrderReturnDetails_SaleOrderReturnId",
                table: "BuyOrderReturnDetails");

            migrationBuilder.DropColumn(
                name: "BuyOrderId",
                table: "SaleOrderDetails");

            migrationBuilder.DropColumn(
                name: "ProductId",
                table: "SaleOrderDetails");

            migrationBuilder.DropColumn(
                name: "SaleOrderReturnId",
                table: "BuyOrderReturnDetails");

            migrationBuilder.RenameColumn(
                name: "UnitPrice",
                table: "SaleOrderDetails",
                newName: "Amount");

            migrationBuilder.RenameColumn(
                name: "Quantity",
                table: "SaleOrderDetails",
                newName: "CustomerId");

            migrationBuilder.RenameColumn(
                name: "SupplierId",
                table: "BuyOrders",
                newName: "CustomerId");

            migrationBuilder.RenameIndex(
                name: "IX_BuyOrders_SupplierId",
                table: "BuyOrders",
                newName: "IX_BuyOrders_CustomerId");

            migrationBuilder.RenameColumn(
                name: "SupplierId",
                table: "BuyOrderReturns",
                newName: "CustomerId");

            migrationBuilder.RenameIndex(
                name: "IX_BuyOrderReturns_SupplierId",
                table: "BuyOrderReturns",
                newName: "IX_BuyOrderReturns_CustomerId");

            migrationBuilder.AddColumn<decimal>(
                name: "Amount",
                table: "SaleOrders",
                type: "TEXT",
                nullable: false,
                defaultValue: 0m);

            migrationBuilder.AddColumn<int>(
                name: "Quantity",
                table: "SaleOrders",
                type: "INTEGER",
                nullable: false,
                defaultValue: 0);

            migrationBuilder.AlterColumn<long>(
                name: "SaleOrderId",
                table: "SaleOrderDetails",
                type: "INTEGER",
                nullable: false,
                defaultValue: 0L,
                oldClrType: typeof(long),
                oldType: "INTEGER",
                oldNullable: true);

            migrationBuilder.AddColumn<string>(
                name: "Code",
                table: "SaleOrderDetails",
                type: "TEXT",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "Name",
                table: "SaleOrderDetails",
                type: "TEXT",
                nullable: true);

            migrationBuilder.CreateIndex(
                name: "IX_SaleOrderDetails_CustomerId",
                table: "SaleOrderDetails",
                column: "CustomerId");

            migrationBuilder.AddForeignKey(
                name: "FK_BuyOrderReturns_Customers_CustomerId",
                table: "BuyOrderReturns",
                column: "CustomerId",
                principalTable: "Customers",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_BuyOrders_Customers_CustomerId",
                table: "BuyOrders",
                column: "CustomerId",
                principalTable: "Customers",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_SaleOrderDetails_Customers_CustomerId",
                table: "SaleOrderDetails",
                column: "CustomerId",
                principalTable: "Customers",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_SaleOrderDetails_SaleOrders_SaleOrderId",
                table: "SaleOrderDetails",
                column: "SaleOrderId",
                principalTable: "SaleOrders",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }
    }
}
